﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using YiYiTong.Rocket.Model;
using YiYiTong.Rocket.Helper;
using YiYiTong.Rocket.Business;
using YiYiTong.Rocket.Entity;
using YiYiTong.Rocket.EFDBFirst;
using YiYiTong.Rocket.Business.Biz;

/*
 * 我是卖家服务
 * 
 * */
namespace YiYiTong.Rocket.Api.Biz
{
    public class SellerService : ApiService
    {

        /// <summary>
        /// 获得卖家商品列表
        /// </summary>
        /// <returns></returns>
        public void getgoodslist(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            int is_withdraw = dic.GetValue<int>("is_withdraw", 0);//0 已上架 1 已下架
            int pageindex = dic.GetValue<int>("pageindex", 1);
            int pagerows = dic.GetValue<int>("pagerows", 10);

            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            //通过uid找到superid
            long uid = apiContext.CurrUserId;
            var seller_apply = dbcontext.ADZ_SellerApply.FirstOrDefault(t => t.uid == uid && t.is_do == 1);
            if (seller_apply.supplier_id > 0)
            {
                int label_type = -10;      //1 直播 2 预热 3 爆款
                long brand_id = -10;        //品牌id
                long category_id = -10;  //分类id
                string selKey = "";             //搜索关键字

                Tuple<bool, GoodsListModel> tupleResult = GoodsBiz.Instance.GetList(apiContext.CurrUserId, label_type, brand_id, category_id, selKey, pageindex, pagerows, is_withdraw, seller_apply.supplier_id.Value,-10);
                if (tupleResult.Item1)
                {
                    apiResponse.retCode = ResultEnum.Success;
                    apiResponse.data = JsonHelper.Serialize(tupleResult.Item2);
                    apiContext.apiHttpResp = apiResponse;
                    return;
                }
            }

            apiResponse.retCode = ResultEnum.NoData;
            apiResponse.message = "商品为空";
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 银行列表
        /// </summary>
        /// <param name="apiContext"></param>
        public void BankList(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            apiResponse.retCode = ResultEnum.Success;
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            var banklist = dbcontext.ADZ_Bank.ToList();
            apiResponse.data = JsonHelper.Serialize(new { banklist = banklist });
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 获取卖家申请状态
        /// </summary>
        public void GetSellerStatus(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            var uid = apiContext.CurrUserId;
            apiResponse.retCode = ResultEnum.Success;
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();

            SellerStatusModel respModel = new SellerStatusModel();

            var seller_apply = dbcontext.ADZ_SellerApply.FirstOrDefault(t => t.uid == uid);
            if (seller_apply == null)
            {
                respModel.apply_status = 0;
                respModel.apply_step = 0;
                respModel.is_do = 0;
            }
            else
            {
                respModel.apply_status = seller_apply.apply_status ?? 0;
                respModel.apply_step = seller_apply.apply_step ?? 0;
                respModel.is_do = seller_apply.is_do ?? 0;
            }
            apiResponse.retCode = ResultEnum.Success;
            apiResponse.data = JsonHelper.Serialize(respModel);

            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 分类列表
        /// </summary>
        /// <param name="apiContext"></param>
        public void GetGoodsCategory(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            apiResponse.retCode = ResultEnum.Success;
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            var goodsCategoryList = dbcontext.ADZ_GoodsCategory.Where(t => t.is_root == 1 && t.is_show == 1 && t.is_del == 0).ToList();
            apiResponse.data = JsonHelper.Serialize(new { list = goodsCategoryList });
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 设置代理商分类
        /// </summary>
        /// <param name="apiContext"></param>
        public void SetSupplierType(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            int supplier_type = dic.GetValue<int>("supplier_type", 0);  //代理商类型

            apiResponse.retCode = ResultEnum.Success;
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();

            var item = dbcontext.ADZ_SupplierType.FirstOrDefault(t => t.supplier_type == supplier_type);
            if (item != null)
            {
                var uid = apiContext.CurrUserId;
                var seller = dbcontext.ADZ_SellerApply.FirstOrDefault(t => t.uid == uid);
                if (seller == null)
                {
                    dbcontext.ADZ_SellerApply.Add(new ADZ_SellerApply
                    {
                        uid = uid,
                        apply_status = 0,
                        apply_step = 1,
                        is_agreement = 0,
                        is_do = 0,
                        create_time = DateTime.Now,
                        supplier_type = supplier_type,
                        supplier_type_name = item.supplier_type_name
                    });
                }
                else
                {
                    seller.supplier_type = supplier_type;
                    seller.supplier_type_name = item.supplier_type_name;
                }
                dbcontext.SaveChanges();
                apiResponse.retCode = ResultEnum.Success;
                apiResponse.data = JsonHelper.Serialize(new { });
            }
            else
            {
                apiResponse.retCode = ResultEnum.Fail;
                apiResponse.message = "传入的参数有误";
            }
            apiContext.apiHttpResp = apiResponse;

        }

        /// <summary>
        /// 设置货品种类 我的主要货品种类为
        /// </summary>
        /// <param name="apiContext"></param>
        public void SetCategoryType(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            string category_ids = dic.GetValue<string>("category_id", "");  //代理商类型

            apiResponse.retCode = ResultEnum.Success;
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();


            var uid = apiContext.CurrUserId;
            var seller = dbcontext.ADZ_SellerApply.FirstOrDefault(t => t.uid == uid);
            if (seller == null)
            {
                dbcontext.ADZ_SellerApply.Add(new ADZ_SellerApply
                {
                    uid = uid,
                    apply_status = 0,
                    apply_step = 2,
                    is_agreement = 0,
                    is_do = 0,
                    create_time = DateTime.Now,
                    category_json = category_ids
                });
            }
            else
            {
                if (seller.apply_step < 2)
                    seller.apply_step = 2;
                seller.category_json = category_ids;
                seller.create_time = DateTime.Now;
            }
            dbcontext.SaveChanges();
            apiResponse.retCode = ResultEnum.Success;
            apiResponse.data = JsonHelper.Serialize(new { });
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 设置同意协议
        /// </summary>
        /// <param name="apiContext"></param>
        public void SetAgreement(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            byte agreement = dic.GetValue<byte>("agreement", 0);  //同意协议0 不同意 1 同意
            int apply_step = 3;
            if (agreement == 0)
            {
                apply_step = 2;
                apiResponse.message = "未同意，不能进下一步";
                apiResponse.data = JsonHelper.Serialize(new { });
                apiContext.apiHttpResp = apiResponse;
                return;
            }
            else
                apply_step = 3;
            apiResponse.retCode = ResultEnum.Success;
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            var uid = apiContext.CurrUserId;
            var seller = dbcontext.ADZ_SellerApply.FirstOrDefault(t => t.uid == uid);
            if (seller == null)
            {
                dbcontext.ADZ_SellerApply.Add(new ADZ_SellerApply
                {
                    uid = uid,
                    apply_status = 0,
                    apply_step = apply_step,
                    is_agreement = agreement,
                    is_do = 0,
                    create_time = DateTime.Now
                });
            }
            else
            {
                if (seller.is_agreement == 0)
                {
                    seller.apply_step = apply_step;
                    seller.is_agreement = agreement;
                    seller.create_time = DateTime.Now;
                }
            }
            dbcontext.SaveChanges();
            if (agreement == 1)
                apiResponse.retCode = ResultEnum.Success;
            else
                apiResponse.message = "未同意，不能进下一步";
            apiResponse.data = JsonHelper.Serialize(new { });

            apiContext.apiHttpResp = apiResponse;
        }


        /// <summary>
        /// 设置基本信息
        /// </summary>
        /// <param name="apiContext"></param>
        public void SetBaseInfo(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            var contact_name = dic.GetValue<string>("contact_name", "");  //联系人
            var telephone = dic.GetValue<string>("telephone", "");  //联系人
            var email = dic.GetValue<string>("email", "");  //联系人
            var qq = dic.GetValue<string>("qq", "");  //联系人
            var company_cityid = dic.GetValue<string>("company_cityid", "000000");  //联系人
            var company_address = dic.GetValue<string>("company_address", "");  //联系人
            var company_cityname = AccountBiz.Instance.GetCityName(company_cityid);

            apiResponse.retCode = ResultEnum.Success;
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            var uid = apiContext.CurrUserId;
            var seller = dbcontext.ADZ_SellerApply.FirstOrDefault(t => t.uid == uid);
            if (seller == null)
            {
                dbcontext.ADZ_SellerApply.Add(new ADZ_SellerApply
                {
                    uid = uid,
                    apply_status = 1,
                    apply_step = 4,
                    is_agreement = 1,
                    is_do = 0,
                    create_time = DateTime.Now,
                    contact_name = contact_name,
                    telephone = telephone,
                    email = email,
                    qq = qq,
                    company_cityid = company_cityid,
                    company_cityname = company_cityname,
                    company_address = company_address
                });
            }
            else
            {
                if (seller.apply_status != 10)
                    seller.apply_status = 1;
                if (seller.apply_step < 4)
                    seller.apply_step = 4;
                seller.contact_name = contact_name;
                seller.telephone = telephone;
                seller.email = email;
                seller.qq = qq;
                seller.company_cityid = company_cityid;
                seller.company_address = company_address;
                seller.create_time = DateTime.Now;
                seller.company_cityname = company_cityname;
            }
            dbcontext.SaveChanges();

            //发送业务消息
            BusinessMsgBiz.Instance.SendBusinessMsg(new ADZ_BusinessMsg
            {
                BusinessMsgType = (int)BusinessMsgEnum.UserApplySell,
                msg = $"用户申请成为卖家，联系人手机号【{telephone}】，联系人【{contact_name}】",
                msg_title = "用户申请成为卖家",
                msg_url = $"/account/sellerapplylist"
            });

            apiResponse.retCode = ResultEnum.Success;
            apiResponse.data = JsonHelper.Serialize(new { });

            apiContext.apiHttpResp = apiResponse;
        }


        /// <summary>
        /// 设置企业信息
        /// </summary>
        /// <param name="apiContext"></param>
        public void SetCompanyInfo(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            var company_name = dic.GetValue<string>("company_name", "");  //企业名称

            var company_img_data = dic.GetValue<string>("company_img_data", "");  //营业执照
            var filetype = dic.GetValue<string>("filetype", "");  //图片类型

            var company_img = "";
            if (!string.IsNullOrWhiteSpace(company_img_data))
            {
                try
                {
                    var result = ImageFileBiz.UpImg(company_img_data, filetype, "companyimg");
                    if (result.Item1)
                    {
                        company_img = result.Item3;
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Error("上传营业执照失败" + ex.GetExcetionMessage());
                }
            }
            apiResponse.retCode = ResultEnum.Success;
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            var uid = apiContext.CurrUserId;
            var seller = dbcontext.ADZ_SellerApply.FirstOrDefault(t => t.uid == uid);
            if (seller == null)
            {
                dbcontext.ADZ_SellerApply.Add(new ADZ_SellerApply
                {
                    uid = uid,
                    apply_status = 1,
                    apply_step = 4,
                    is_agreement = 1,
                    is_do = 0,
                    create_time = DateTime.Now,
                    company_name = company_name,
                    company_img = company_img
                });
            }
            else
            {
                if (seller.apply_step < 5)
                    seller.apply_step = 5;
                if (seller.apply_status != 10)
                    seller.apply_status = 1;
                seller.create_time = DateTime.Now;
                seller.company_name = company_name;
                if (!string.IsNullOrWhiteSpace(company_img))
                {
                    seller.company_img = company_img;
                }
            }
            dbcontext.SaveChanges();

            //发送业务消息
            BusinessMsgBiz.Instance.SendBusinessMsg(new ADZ_BusinessMsg
            {
                BusinessMsgType = (int)BusinessMsgEnum.ApplySellModifyInfo,
                msg = $"卖家申请提交企业信息，联系人手机号【{seller.telephone}】，企业名称【{seller.company_name}】，联系人【{seller.contact_name}】",
                msg_title = "卖家申请提交企业信息",
                msg_url = $"/account/sellerapplylist"
            });

            apiResponse.retCode = ResultEnum.Success;
            apiResponse.data = JsonHelper.Serialize(new { });

            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 获取卖家申请信息
        /// </summary>
        /// <param name="apiContext"></param>
        public void GetSellerApply(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;
            var uid = apiContext.CurrUserId;
            apiResponse.retCode = ResultEnum.Success;
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            var info = dbcontext.ADZ_SellerApply.FirstOrDefault(t => t.uid == uid);
            if (info != null)
            {
                if (!string.IsNullOrWhiteSpace(info.company_img))
                {
                    info.company_img = ImageFileBiz.GetImg(info.company_img, "small");
                }
            }
            apiResponse.data = JsonHelper.Serialize(info);
            apiContext.apiHttpResp = apiResponse;

        }

        /// <summary>
        /// 修改全部申请信息
        /// </summary>
        /// <param name="apiContext"></param>
        public void SetAllApplyInfo(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);

            var contact_name = dic.GetValue<string>("contact_name", "");  //联系人
            var telephone = dic.GetValue<string>("telephone", "");  //联系人
            var email = dic.GetValue<string>("email", "");  //联系人
            var qq = dic.GetValue<string>("qq", "");  //联系人
            var company_cityid = dic.GetValue<string>("company_cityid", "");  //联系人
            var company_cityname = AccountBiz.Instance.GetCityName(company_cityid);
            var company_address = dic.GetValue<string>("company_address", "");  //联系人
            var company_name = dic.GetValue<string>("company_name", "");  //企业名称
            var company_img_data = dic.GetValue<string>("company_img_data", "");  //营业执照
            var filetype = dic.GetValue<string>("filetype", "");  //图片类型
            var supplier_type = dic.GetValue<int>("supplier_type", 0);  //卖家类型

            var company_img = "";
            if (!string.IsNullOrWhiteSpace(company_img_data) && company_img_data.Length > 30)
            {
                try
                {
                    var result = ImageFileBiz.UpImg(company_img_data, filetype, "companyimg");
                    if (result.Item1)
                    {
                        company_img = result.Item3;
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Error("上传营业执照失败" + ex.GetExcetionMessage());
                }
            }
            apiResponse.retCode = ResultEnum.Success;
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            var uid = apiContext.CurrUserId;
            var seller = dbcontext.ADZ_SellerApply.FirstOrDefault(t => t.uid == uid);
            if (seller == null)
            {
                seller = new ADZ_SellerApply
                {
                    uid = uid,
                    apply_status = 1,
                    apply_step = 5,
                    is_agreement = 1,
                    is_do = 0,
                    create_time = DateTime.Now
                };
            }
            if (seller.supplier_type != supplier_type)
            {
                var item = dbcontext.ADZ_SupplierType.FirstOrDefault(t => t.supplier_type == supplier_type);
                if (item != null)
                {
                    seller.supplier_type = supplier_type;
                    seller.supplier_type_name = item.supplier_type_name;
                }
            }


            if (seller.apply_step < 5)
                seller.apply_step = 5;
            if (seller.apply_status != 10)
                seller.apply_status = 1;
            seller.company_name = company_name;
            if (!string.IsNullOrWhiteSpace(company_img))
            {
                seller.company_img = company_img;
            }
            seller.contact_name = contact_name;
            seller.telephone = telephone;
            seller.email = email;
            seller.qq = qq;
            seller.company_cityid = company_cityid;
            seller.company_cityname = company_cityname;
            seller.company_address = company_address;
            seller.create_time = DateTime.Now;
            if (seller.seq_id == 0)
            {
                dbcontext.ADZ_SellerApply.Add(seller);
            }

            dbcontext.SaveChanges();

            apiResponse.retCode = ResultEnum.Success;
            apiResponse.data = JsonHelper.Serialize(new { });

            apiContext.apiHttpResp = apiResponse;
        }


        /// <summary>
        /// 卖家详情
        /// </summary>
        /// <param name="apiContext"></param>
        public void GetSupplierInfo(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            apiResponse.retCode = ResultEnum.Success;
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            var uid = apiContext.CurrUserId;
            var seller_apply = dbcontext.ADZ_SellerApply.FirstOrDefault(t => t.uid == uid && t.is_do == 1);
            if (seller_apply != null)
            {
                if (seller_apply.supplier_id > 0)
                {
                    var masterinfo = dbcontext.ADZ_SupplierMaster.FirstOrDefault(t => t.supplier_id == seller_apply.supplier_id);

                    if (!string.IsNullOrWhiteSpace(masterinfo.zhizhao_url))
                    {
                        masterinfo.zhizhao_url = ImageFileBiz.GetImg(masterinfo.zhizhao_url, "small");
                    }

                    masterinfo.city_name = AccountBiz.Instance.GetCityName(masterinfo.cityId);
                    apiResponse.retCode = ResultEnum.Success;

                    apiResponse.data = JsonHelper.Serialize(masterinfo);
                    apiContext.apiHttpResp = apiResponse;
                }
            }
        }
        /// <summary>
        /// 修改卖家详情信息
        /// </summary>
        /// <param name="apiContext"></param>
        public void SetSupplierInfo(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;
            apiResponse.retCode = ResultEnum.Success;
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();

            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            var contact_name = dic.GetValue<string>("contact_name", "");  //联系人
            var telephone = dic.GetValue<string>("telephone", "");  //联系人
            var email = dic.GetValue<string>("email", "");  //联系人
            var qq = dic.GetValue<string>("qq", "");  //联系人
            var cityId = dic.GetValue<string>("cityId", "");  //联系人
            var supplier_address = dic.GetValue<string>("supplier_address", "");  //联系人
            var supplier_name = dic.GetValue<string>("supplier_name", "");  //企业名称
            var zhizhao_url_data = dic.GetValue<string>("zhizhao_url_data", "");  //营业执照
            var filetype = dic.GetValue<string>("filetype", "");  //图片类型
            var supplier_type = dic.GetValue<byte>("supplier_type", 0);  //卖家类型

            var zhizhao_url = "";
            if (!string.IsNullOrWhiteSpace(zhizhao_url_data) && zhizhao_url_data.Length > 30)
            {
                try
                {
                    var result = ImageFileBiz.UpImg(zhizhao_url_data, filetype, "companyimg");
                    if (result.Item1)
                    {
                        zhizhao_url = result.Item3;
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Error("上传营业执照失败" + ex.GetExcetionMessage());
                }
            }
            var uid = apiContext.CurrUserId;
            var seller_apply = dbcontext.ADZ_SellerApply.FirstOrDefault(t => t.uid == uid && t.is_do == 1);
            ADZ_SupplierMaster supplier = new ADZ_SupplierMaster();
            if (seller_apply != null)
            {
                supplier = dbcontext.ADZ_SupplierMaster.FirstOrDefault(t => t.supplier_id == seller_apply.supplier_id);
                if (supplier != null)
                {
                    supplier.contact_name = contact_name;
                    supplier.telephone = telephone;
                    supplier.email = email;
                    supplier.telephone = telephone;
                    supplier.email = email;
                    supplier.qq = qq;
                    supplier.cityId = cityId;
                    supplier.supplier_address = supplier_address;
                    supplier.supplier_type = supplier_type;
                    if (!string.IsNullOrWhiteSpace(zhizhao_url))
                        supplier.zhizhao_url = zhizhao_url;
                    supplier.status = 3;
                }
            }

            dbcontext.SaveChanges();

            //发送业务消息
            BusinessMsgBiz.Instance.SendBusinessMsg(new ADZ_BusinessMsg
            {
                BusinessMsgType = (int)BusinessMsgEnum.SellerModifyInfo,
                msg = $"卖家修改信息，联系人手机号【{supplier.telephone}】，企业名称【{supplier.supplier_name}】，联系人【{supplier.contact_name}】",
                msg_title = "卖家修改信息",
                msg_url = $"/supplier/supplierlist"
            });

            apiResponse.retCode = ResultEnum.Success;
            apiResponse.data = JsonHelper.Serialize(new { });

            apiContext.apiHttpResp = apiResponse;
        }

        #region 支付信息

        /// <summary>
        /// 增加收款账户
        /// </summary>
        /// <param name="apiContext"></param>
        public void AddPayAccount(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;
            apiResponse.retCode = ResultEnum.Success;
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();

            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);


            var seq_id = dic.GetValue<long>("seq_id", 0);  //判断修改还是增加
            var channel = dic.GetValue<byte>("channel", 0);  //
            var bank_id = dic.GetValue<int>("bank_id", 0);  //银行编号
            var branch_bank_name = dic.GetValue<string>("branch_bank_name", "");  //branch_bank_name;
            var bank_account_name = dic.GetValue<string>("bank_account_name", "");  // bank_account_name;
            var bank_account = dic.GetValue<string>("bank_account", "");  //bank_account;
            var wechat_account = dic.GetValue<string>("wechat_account", "");  //wechat_account;
            var alipay_account = dic.GetValue<string>("alipay_account", "");  // alipay_account;

            var currency_type = dic.GetValue<byte>("currency_type", 0);  //0 人民币 1美元
            var is_invoice = dic.GetValue<byte>("is_invoice", 0);  //0不要发票 1要发票
            var bank_acc_type = dic.GetValue<byte>("bank_acc_type", 0);  //1对公账户  2对私账户


            var supplier_id = 0L;
            var uid = apiContext.CurrUserId;
            var seller_apply = dbcontext.ADZ_SellerApply.FirstOrDefault(t => t.uid == uid && t.is_do == 1);
            if (seller_apply != null)
            {
                supplier_id = seller_apply.supplier_id.Value;
            }

            ADZ_SupplierDetails details = new ADZ_SupplierDetails();
            if (seq_id > 0)
            {
                details = dbcontext.ADZ_SupplierDetails.FirstOrDefault(t => t.seq_id == seq_id);
                if (details == null || details.supplier_id != supplier_id)
                {
                    ///传入的信息不对称
                    apiResponse.retCode = ResultEnum.Fail;
                    apiResponse.message = "id和信息不对称";
                    apiResponse.data = JsonHelper.Serialize(new { });
                    apiContext.apiHttpResp = apiResponse;
                    return;
                }
            }
            else
            {
                details.supplier_id = supplier_id;
                details.audit_status = 0;
                details.is_del = 0;
            }


            //通过银行id获取银行名称
            if (bank_id > 0)
            {
                var bank = dbcontext.ADZ_Bank.FirstOrDefault(t => t.bank_id == bank_id);
                if (bank != null)
                {
                    details.bank_name = bank.bank_name;
                    details.bank_id = bank_id;
                }
            }
            //收款渠道 1 银行转账 2 支付宝 3 微信
            switch (channel)
            {
                case 1:
                    details.bank_id = bank_id;
                    details.branch_bank_name = branch_bank_name;
                    details.bank_account_name = bank_account_name;
                    details.bank_account = bank_account;
                    details.wechat_account = wechat_account;

                    break;
                case 2:
                    details.alipay_account = alipay_account;
                    break;
                case 3:
                    details.wechat_account = wechat_account;
                    break;
            }
            details.currency_type = currency_type;
            details.is_invoice = is_invoice;
            details.bank_acc_type = bank_acc_type;
            details.channel = channel;
            if (seq_id == 0)
            {
                dbcontext.ADZ_SupplierDetails.Add(details);
            }
            dbcontext.SaveChanges();

            var supplier = dbcontext.ADZ_SupplierMaster.FirstOrDefault(t => t.supplier_id == supplier_id);
            //发送业务消息
            BusinessMsgBiz.Instance.SendBusinessMsg(new ADZ_BusinessMsg
            {
                BusinessMsgType = (int)BusinessMsgEnum.SellerSubmitPayaccount,
                msg = $"卖家维护收款账户，联系人手机号【{supplier.telephone}】，企业名称【{supplier.supplier_name}】，联系人【{supplier.contact_name}】",
                msg_title = "卖家维护收款账户",
                msg_url = $"/supplier/supplierlist?supplier_name={supplier.supplier_name}"
            });


            apiResponse.retCode = ResultEnum.Success;
            apiResponse.data = JsonHelper.Serialize(new { });
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 获取支付账户信息
        /// </summary>
        /// <param name="apiContext"></param>
        public void GetPayAccount(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;
            apiResponse.retCode = ResultEnum.Success;
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();

            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);


            var seq_id = dic.GetValue<long>("seq_id", 0);  //id

            var uid = apiContext.CurrUserId;
            var seller_apply = dbcontext.ADZ_SellerApply.FirstOrDefault(t => t.uid == uid && t.is_do == 1);
            if (seller_apply != null)
            {
                var supplier = dbcontext.ADZ_SupplierMaster.FirstOrDefault(t => t.supplier_id == seller_apply.supplier_id);
                if (supplier != null)
                {
                    var supplierDetail = dbcontext.ADZ_SupplierDetails.FirstOrDefault(t => t.seq_id == seq_id && t.supplier_id == supplier.supplier_id);
                    apiResponse.data = JsonHelper.Serialize(supplierDetail);
                    apiContext.apiHttpResp = apiResponse;
                    return;
                }
            }

            apiResponse.retCode = ResultEnum.NoData;
            apiResponse.data = "{}";
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 获取支付列表
        /// </summary>
        /// <param name="apiContext"></param>
        public void GetPayList(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            apiResponse.retCode = ResultEnum.Success;
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            var uid = apiContext.CurrUserId;
            var seller_apply = dbcontext.ADZ_SellerApply.FirstOrDefault(t => t.uid == uid && t.is_do == 1);
            if (seller_apply != null)
            {
                var supplier = dbcontext.ADZ_SupplierMaster.FirstOrDefault(t => t.supplier_id == seller_apply.supplier_id);
                if (supplier != null)
                {
                    var paylist = dbcontext.ADZ_SupplierDetails.Where(t => t.is_del == 0 && t.supplier_id == supplier.supplier_id).ToList();
                    apiResponse.data = JsonHelper.Serialize(new { paylist = paylist });
                    apiContext.apiHttpResp = apiResponse;
                    return;
                }
            }

            apiResponse.retCode = ResultEnum.NoData;
            apiResponse.data = "[]";
            apiContext.apiHttpResp = apiResponse;
        }

        #endregion

        /// <summary>
        /// 我是卖家首页
        /// </summary>
        /// <param name="apiContext"></param>
        public void SupplierIndex(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            apiResponse.retCode = ResultEnum.Success;
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            var uid = apiContext.CurrUserId;
            var supplier_id = 0L;
            var seller_apply = dbcontext.ADZ_SellerApply.FirstOrDefault(t => t.uid == uid && t.is_do == 1);
            if (seller_apply != null)
            {
                supplier_id = seller_apply.supplier_id ?? 0;
            }
            if (supplier_id <= 0)
            {
                apiResponse.retCode = ResultEnum.NoData;
                apiResponse.message = "卖家信息还未通过审核";
                apiContext.apiHttpResp = apiResponse;
                return;
            }
            var model = new SupplierIndexModel();
            model.Supplier = dbcontext.ADZ_SupplierMaster.FirstOrDefault(t => t.supplier_id == supplier_id);
            model.Supplier.remark = seller_apply.supplier_type_name;

            var sellinglist = dbcontext.ADZ_GoodsMaster.Where(t => t.supplier_id == supplier_id);

            model.AllSelleringRMB = sellinglist.Where(t => t.currency_type == 0 && t.is_withdraw == 0 && t.is_del == 0).Sum(t => t.price) ?? 0;
            model.AllSelleringUS = sellinglist.Where(t => t.currency_type == 1 && t.is_withdraw == 0 && t.is_del == 0).Sum(t => t.price) ?? 0;

            ////已售列表
            var selledlist = (from tba in dbcontext.ADZ_OrderMaster.Where(t => t.is_succ == 1)
                              join tbb in dbcontext.ADZ_OrderDetails on tba.order_id equals tbb.order_id
                              join tbc in dbcontext.ADZ_GoodsMaster.Where(t => t.supplier_id == supplier_id) on tbb.goods_id equals tbc.goods_id
                              select new
                              {
                                  tbc.currency_type,
                                  tbb.price
                              }).ToList();

            model.AllSelleredRMB = selledlist.Where(t => t.currency_type == 0).Sum(t => t.price) ?? 0;
            model.AllSelleredUS = selledlist.Where(t => t.currency_type == 1).Sum(t => t.price) ?? 0;

            model.manager_url = ConfigHelper.AppSetting<string>("managerUrl");
            model.manager_name = seller_apply.telephone;

            apiResponse.retCode = ResultEnum.Success;
            apiResponse.data = JsonHelper.Serialize(model);
            apiContext.apiHttpResp = apiResponse;
        }
    }

    public class SupplierIndexModel
    {
        public ADZ_SupplierMaster Supplier { get; set; }
        /// <summary>
        /// 已销售人民币
        /// </summary>
        public decimal AllSelleredRMB { get; set; }
        /// <summary>
        /// 已销售 美元
        /// </summary>
        public decimal AllSelleredUS { get; set; }
        /// <summary>
        /// 销售中 人民币
        /// </summary>
        public decimal AllSelleringRMB { get; set; }
        /// <summary>
        /// 销售中  美元
        /// </summary>
        public decimal AllSelleringUS { get; set; }

        public string manager_url { get; set; }
        public string manager_name { get; set; }
    }
}